var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var circleUrl = require("url/circle");
var http = require("helper/http");

//声明UI变量
var listview=ui("do_SlideListView_1");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据

// UI
var headerAout = ui("headerAout");
var linearLayout_main = ui("linearLayout_main")

// 加载头部---begin
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
page.on("back", function() {
	app.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
headerLeft.on("touch", function() {
	app.closePage({memberGrade_updata:true})
});
//右上角-发布（提交）
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "完成";
headerRight_lableNext.visible = true;
headerRight.on("touch", function() {
	submitModifyGrade();
});
//头部-中间-标题
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "设置成员头衔",
})
header.refreshData();
//加载头部---end

var id = null;//等级ID（key）
var circleId = null;
var gradeCode = null;
//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	id = page.getData().id;
	loadPage();
});

page.on("member_remove_grade", function(){
	loadMemberList();
});

page.on("result", function(data) {
	var type = data.type;
	if(type == "selectCircleMember"){
		setGradeBath(data.members);
	}
});


ui("do_ALayout_del").on("touch",function(){
	ui("textGradeName").text = "";
});

//批量-设置头衔
function setGradeBath(members){
	if(members != null && members.length>0){
		var memberIds = [];
		for(var i=0;i<members.length;i++){
			memberIds.push(members[i].memberId);
			members[i].template=0;
			members[i].rightTemplate=1;
		}
		if(memberIds.length>0){
			var memberIdsStr = memberIds.join(";");
			var params = {
					"circleId": circleId+"",
					"gradeCode": gradeCode+"",
					"memberIds": memberIdsStr
					};
			http.post(circleUrl.url.member.setGradeBath,params,function(data){
				var positionBeforeLast = listdata.getCount()-1;
				if(positionBeforeLast<0){
					positionBeforeLast = 0;
				}
				listdata.addData(members,positionBeforeLast);
				listview.refreshItems();//刷新item数据
				nf.toast("设置成员头衔...成功");
			});
		}
		
		
	}
}

function loadPage(){
	http.post(circleUrl.url.memberGrade.detail,{"id":id},function(data){
		var memberGrade = data.memberGrade;
		if(memberGrade){
			circleId = memberGrade.circleId;
			gradeCode = memberGrade.gradeCode;
			ui("textGradeName").text = memberGrade.gradeName;
			loadMemberList();
		}
	});
}


function loadMemberList(){
	if(circleId==null || gradeCode ==null){
		return;
	}
	
	http.post(circleUrl.url.member.members4Grade,{"circleId":circleId,"gradeCode":gradeCode},function(data){
		var memberList = data.memberList;
		listdata.removeAll();//清除数据
		if(memberList.length){
			for(var i=0;i<memberList.length;i++){
				memberList[i].template=0;
				memberList[i].rightTemplate=1;
			}
			listdata.addData(memberList);
			listview.refreshItems();//刷新item数据
		}
		listdata.addOne({"template":2,"circleId":circleId,"gradeCode":gradeCode});//最后一行 "+" 号
		listview.refreshItems();//刷新item数据
	});
}

function submitModifyGrade(){
	var gradeName = ui("textGradeName").text;
	if(gradeName == "" || gradeName.length ==0){
		nf.toast("请输入等级名称");
		return;
	}
	http.post(circleUrl.url.memberGrade.update,{"id":id,"gradeName":gradeName},function(data){
		nf.toast("修改成功！");	
		app.closePage({memberGrade_updata:true})
	});
}
